﻿using Ext.Net;
using NOAH.CMS.Cache;
using NOAH.CMS.Repository;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

public partial class admin_Modules_NewsEvent_NewsCategory_Sort : System.Web.UI.Page
{
    protected void LoadOrderForm()
    {
        using (NewsCategoryRepository repository = new NewsCategoryRepository())
        {
            string id = base.Request.QueryString["id"] ?? "0";
          
            
            this.hdfParentCategoryID.Value = id;
            string cates = "";
            List<NewsCategory> allCate = repository.GetAll();
            List<NewsCategory> childCates = new List<NewsCategory>();
            if (id == "0")
            {
                childCates = (from p in allCate.FindAll(p => p.ParentId == 0)
                              orderby p.Order
                              select p).ToList<NewsCategory>();
            }
            else
            {
                childCates = (from p in allCate.FindAll(p => p.ParentId.ToString() == id)
                              orderby p.Order
                              select p).ToList<NewsCategory>();
            }
            foreach (NewsCategory cate in childCates)
            {
                cates = cates + string.Format("<li id='{0}'>{1}</li>", cate.ID.ToString(), cate.Name);
            }
            string output = " <div class='demo'>\r\n                            <ul id='sortable'>\r\n                             {0}\r\n                            </ul>\r\n                        </div>";
            string script = " $(function () {\r\n                                $('#sortable').sortable();\r\n                                $('#sortable').disableSelection();\r\n                            });";
            this.lblCategoryOrder.Text = string.Format(output, cates);
            this.SM.RegisterClientScriptBlock("load", script);
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!ExtNet.IsAjaxRequest)
        {
            this.LoadOrderForm();
        }
    }

    public void UpdateNewsCategoryOrder(object sender, DirectEventArgs e)
    {
        NewsCategoryRepository repository = new NewsCategoryRepository();
        try
        {
            short k;
            string order = e.ExtraParams["order"];
            string parentId = base.Request.QueryString["id"] ?? "0";
          
            List<NewsCategory> allCate = repository.GetAll();
            if ((parentId == "0") || string.IsNullOrEmpty(parentId))
            {
                List<NewsCategory> topCategories = allCate.FindAll(p => p.ParentId.ToString() == parentId).ToList<NewsCategory>();
                if (topCategories.Count == 0)
                {
                    Util.ShowMessage2("Kh\x00f4ng c\x00f3 chuy\x00ean mục n\x00e0o đế sắp xếp!");
                    return;
                }
                k = 0;
                foreach (string id in order.Split(new char[] { ',' }))
                {
                    topCategories.Find(p => p.ID.ToString() == id).Order = new short?(k);
                    k = (short)(k + 1);
                }
                repository.SubmitChanges();
            }
            else
            {
                List<NewsCategory> cates = allCate.FindAll(p => p.ParentId == int.Parse(parentId)).ToList<NewsCategory>();
                if (cates.Count == 0)
                {
                    Util.ShowMessage2("Kh\x00f4ng c\x00f3 chuy\x00ean mục n\x00e0o đế sắp xếp!");
                    return;
                }
                k = 0;
                foreach (string id in order.Split(new char[] { ',' }))
                {
                    cates.Find(p => p.ID.ToString() == id).Order = new short?(k);
                    k = (short)(k + 1);
                }
                repository.SubmitChanges();
            }
            new NewsCategoryCacheLayer().InvalidateCache();
            BasePage.RegisterCallBackFromChild(this.SM, string.Empty);
            BasePage.RegisterCloseWindow(this.SM);
        }
        catch(Exception ex)
        {
            Util.ShowMessage2(Constants.UPDATE_FAILURE + " : " + ex.Message + " : "+ ex.StackTrace);
        }
        finally
        {
            if (repository != null)
            {
                repository.Dispose();
            }
        }
    }
}